package com.usian.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.usian.mapper.LogMapper;
import com.usian.mapper.MemberMapper;
import com.usian.service.IMemberService;
import com.usian.shop.entity.Log;
import com.usian.shop.entity.Member;
import com.usian.shop.utils.ResponseResult;
import org.springframework.stereotype.Controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-01-10
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private IMemberService memberService;
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private LogMapper logMapper;
    @RequestMapping("/login")
    public ResponseResult login(@RequestBody Member member){
return memberService.login(member);
    }
    @GetMapping("/findAll")
    public Member findAll(@RequestParam("name") String name){
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",name);
        return memberService.getOne(queryWrapper);
    }
    @PostMapping("/update")
    public ResponseResult update(@RequestBody Member member){
        QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",member.getName());
        Member member1 = memberService.getOne(queryWrapper);
        member1.setBmoney(member1.getBmoney()+member.getMoney());

        try {
            memberService.updateById(member1);
            Log log = new Log();
            log.setType("充值");
            log.setCmoney(member.getMoney());
            log.setBmoney(member1.getBmoney()-member.getMoney());
            log.setHmoney(member1.getBmoney());
            log.setCreatetime(new Date());
            logMapper.insert(log);
            return ResponseResult.errorResult(200,"充值成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(500,"充值失败");
        }
    }
}
